/**
 * Created by Administrator on 2016/9/9.
 */
if ('addEventListener' in document) {
    document.addEventListener('DOMContentLoaded', function() {
        // 绑定事件到document.body
        FastClick.attach(document.body);
    }, false);
}
var price
var a = 0
$('.recharge').on('click',function(){
    $(this).addClass('selected').siblings().removeClass('selected')
    $('#charge').removeClass('red')
    if (!($(this).hasClass('has-input')) || $('.recharge input').val() != '') {
        $('#charge').addClass('red')
    }
    var a = $(this).find('.color .big').text()
    var input_value = $(this).find('input').val()
    price = input_value || a
    $('span.charge-money').text(price)
    $('.box-charge-money').text('￥'+price)
})
$('.recharge input').keyup(input)
function input(){
    price = this.value
    if (price != '') {
        $('#charge').addClass('red')
    }else{
        $('#charge').removeClass('red')
    }
    var back = $(this).next().find('.get-money')
    var balance = $(this).next().find('span.discount')
    var total = $(this).next().find("span.total")
    if(price == 5000){
        back.text('送250元，')
        balance.text('95折，')
        total.text(5250)
    }else if(price == 15000){
        back.text('送1666元，')
        balance.text('90折，')
        total.text(16666)
    }else if(price == 49999){
        back.text('送12500元，')
        balance.text('80折，')
        total.text(62499)
    }else if(price<5000){
        back.text('送0元，')
        balance.text('10折，')
        total.text(+price)
    }else if(price>5000&&price<15000){
        var money = Math.floor(price*0.05)
        back.text('送'+money+'元，')
        balance.text('95折，')
        total.text(+money + +price)
    }else if(price>15000&&price<49999){
        var money1 = Math.floor(price*0.11)
        back.text('送'+money1+'元，')
        balance.text('90折，')
        total.text(+money1 + +price)
    }else if(price>49999){
        var money2 = Math.floor(price*0.25)
        back.text('送'+money2+'元，')
        balance.text('80折，')
        total.text(+money2 + +price)    }
    $('span.charge-money').text(price)
    $('.box-charge-money').text('￥'+price)
}
$('#charge').on('click',function(){
    if($(this).hasClass('red')){
        $('#contentBox').addClass('background')
        $('.choose-box').removeClass('display')
    }else{
        alert('请输入金额')
    }
})
$('#contentBox').on('click',function(){
    $('#contentBox').removeClass('background')
    $('.choose-box').addClass('display')
    $('.push-phone').addClass('display')
    $('.confirm-box').addClass('display')
})
$('.charge-list').on('click',function(){
    $(this).find('.yuan').addClass('m-xuanze').removeClass('m-circle')
    $(this).siblings().find('.yuan').removeClass('m-xuanze').addClass('m-circle')
    $(this).siblings('#zhifu').addClass('red')
    if($(this).hasClass('wx-charge')){
        $('#zhifu').addClass('weixin').removeClass('bank')
    }else if($(this).hasClass('bank-charge')){
        $('#zhifu').addClass('bank').removeClass('weixin')
    }
    a = 1
})
$('#checkbox').on('click',function(){
    if(this.checked){
        $('#zhifu').addClass('check')
        a += 1
    }else{
        $('#zhifu').removeClass('check')
        a -= 1
    }
})
$('#zhifu').on('click',function(){
   if(a == 1 && $(this).hasClass('weixin')){
        //微信支付接口
        $.ajax({
            type: "post",
            url: '/mohe/testpay/',
            data: {
                'money': price,
                ci_csrf_token: ci_csrf_token(),

            },
            dataType:'json',
            success: function(msg){
                if (msg.status == 200) {
                    var info = msg.msg;
                    wxConf(info.js)
                    weChat(info.config)
                }else{
                    alert(msg.msg);
                }
            },
            fail:function(msg){
                // $('#contentBox').text(JSON.stringify(msg))
            },
        });
    }else if(a == 1 && $(this).hasClass('bank')){
        $('.choose-box').addClass('display')
        $('.push-phone').removeClass('display').find('input.tele').focus()
    }
})
$('.close').on('click',function(){
    $('#contentBox').removeClass('background')
    $('.push-phone').addClass('display')
})
$('.push-phone button').on('click',function(){
    var chargeMoney = $('.charge-money').text()
    var phoneNumber = $('.push-phone .tele').val()
    // 提交数据
    $.post('/mohe/front/mohe/ajaxCharge', {money: chargeMoney, telephone: phoneNumber, ci_csrf_token: ci_csrf_token()}, function(data) {
        if (data.status == 'ok') {
            $('.push-phone').addClass('display')
            $('.confirm-box').removeClass('display')
        } else if (data.status == 'error') {
            alert(data.msg);
        }
    }, 'json')
})
$('button.confirm').on('click',function(){
    $('#contentBox').removeClass('background')
    $('.confirm-box').addClass('display')
})
function wxConf(info){
    wx.config(info)
}
function weChat(data){
    wx.chooseWXPay({  
        timestamp: data.timestamp,//<?php echo $config["timestamp"];?>支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符  
        nonceStr: data.nonceStr,//'<?php echo $config["nonceStr"]; ?>'支付签名随机串，不长于 32 位  
        package: data.package,//'<?php echo $config["package"]; ?>'统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）  
        signType: data.signType,//'<?php echo $config["signType"]; ?>'签名方式，默认为'SHA1'，使用新版支付需传入'MD5'  
        paySign: data.paySign,//'<?php echo $config["paySign"]; ?>'支付签名  
        success: function (res) {  
            // 支付成功后的回调函数  
            if(res.err_msg == "get_brand_wcpay_request：ok" ) {  
                alert('支付成功');  
                window.location.href="http://www.udfang.com/mohe/account"; 
            }
            if(res.errMsg == "chooseWXPay:ok" ) {
                alert('支付成功');  
                window.location.href="http://www.udfang.com/mohe/account";
            }else{  
                alert(res.errMsg);
            }  
        },  
        fail: function (res) { 
            alert("支付失败，请返回重试。fail");  
            alert(res.errMsg);   
        }  
    });
}